package org.zhaozhen.quartz.service.impl;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.zhaozhen.quartz.job.MainJob;
import org.zhaozhen.quartz.service.IQuartzService;
import org.zhaozhen.quartz.util.QuartzJobInfo;
import org.zhaozhen.quartz.util.QuartzUtils;
@Service
public class QuartzServiceImpl implements IQuartzService {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public void addJob(QuartzJobInfo jobInfo) {
        Scheduler sched = schedulerFactoryBean.getScheduler();
        String jobName = jobInfo.getJobName();
        Class cls = MainJob.class;
        Object params = jobInfo.getParams();
        String time = jobInfo.getCronj();
        QuartzUtils.addJob(sched,jobName,cls,params,time);
        System.out.println("动态添加定时任务:"+jobName);
    }

    @Override
    public void removeJob(String jobName) {
        Scheduler sched = schedulerFactoryBean.getScheduler();
        QuartzUtils.removeJob(sched,jobName);
        System.out.println("删除还未执行的定时任务:"+jobName);

    }

}
